package com.ruoyi.flow.service.impl;

import java.io.InputStream;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import com.ruoyi.audit.enums.BpmnTypeEnum;
import com.ruoyi.audit.utils.ActivitiUtils;
import com.ruoyi.flow.domain.vo.BusBpmnInfoVo;
import com.ruoyi.flow.service.IProcessService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.flow.mapper.BusBpmnInfoMapper;
import com.ruoyi.flow.domain.BusBpmnInfo;
import com.ruoyi.flow.service.IBusBpmnInfoService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

/**
 * 流程定义Service业务层处理
 *
 * @author ruoyi
 * @date 2023-10-26
 */
@Service
public class BusBpmnInfoServiceImpl implements IBusBpmnInfoService {
    @Autowired
    private BusBpmnInfoMapper busBpmnInfoMapper;
    @Autowired
    private IProcessService processService;

    /**
     * 查询流程定义
     *
     * @param id 流程定义主键
     * @return 流程定义
     */
    @Override
    public BusBpmnInfo selectBusBpmnInfoById(Long id) {
        return busBpmnInfoMapper.selectBusBpmnInfoById(id);
    }

    /**
     * 查询流程定义列表
     *
     * @param busBpmnInfo 流程定义
     * @return 流程定义
     */
    @Override
    public List<BusBpmnInfo> selectBusBpmnInfoList(BusBpmnInfo busBpmnInfo) {
        return busBpmnInfoMapper.selectBusBpmnInfoList(busBpmnInfo);
    }

    /**
     * 新增流程定义
     *
     * @param busBpmnInfo 流程定义
     * @return 结果
     */
    @Override
    public int insertBusBpmnInfo(BusBpmnInfo busBpmnInfo) {
        return busBpmnInfoMapper.insertBusBpmnInfo(busBpmnInfo);
    }

    /**
     * 修改流程定义
     *
     * @param busBpmnInfo 流程定义
     * @return 结果
     */
    @Override
    public int updateBusBpmnInfo(BusBpmnInfo busBpmnInfo) {
        return busBpmnInfoMapper.updateBusBpmnInfo(busBpmnInfo);
    }

    /**
     * 批量删除流程定义
     *
     * @param ids 需要删除的流程定义主键
     * @return 结果
     */
    @Override
    public int deleteBusBpmnInfoByIds(Long[] ids) {
        Assert.state(ids.length == 1, "id参数有误");
        BusBpmnInfo busBpmnInfo = selectBusBpmnInfoById(ids[0]);
        processService.deleteDefinition(busBpmnInfo.getProcessDefinitionKey(), busBpmnInfo.getVersion());
        return busBpmnInfoMapper.deleteBusBpmnInfoByIds(ids);
    }

    /**
     * 删除流程定义信息
     *
     * @param id 流程定义主键
     * @return 结果
     */
    @Override
    public int deleteBusBpmnInfoById(Long id) {
        return busBpmnInfoMapper.deleteBusBpmnInfoById(id);
    }

    /**
     * 新增流程定义部署
     *
     * @param busBpmnInfoVo
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deployByBpmnInfo(BusBpmnInfoVo busBpmnInfoVo) {
        BusBpmnInfo busBpmnInfo = processService.deploy(busBpmnInfoVo.getBpmnFileName(), busBpmnInfoVo.getBpmnFileStream());
        busBpmnInfo.setBpmnType(busBpmnInfoVo.getBpmnType());
        busBpmnInfo.setInfo(busBpmnInfoVo.getInfo());
        busBpmnInfoMapper.insertBusBpmnInfo(busBpmnInfo);

    }

    /**
     * 获取流程文件输入流
     *
     * @param id
     * @param type
     * @return
     */
    @Override
    public InputStream selectBpmnInfoFile(Long id, String type) {
        BusBpmnInfo busBpmnInfo = selectBusBpmnInfoById(id);
        return processService.getResourceAsStream(busBpmnInfo.getProcessDefinitionKey(), busBpmnInfo.getVersion(), type);

    }

    /**
     * 获取流程定义价格
     *
     * @return
     */
    @Override
    public BigDecimal getCarPackageAuditDiscountPrice() {
        BusBpmnInfo busBpmnInfo = busBpmnInfoMapper.selectLastestBpmnInfoByType(BpmnTypeEnum.CAR_PACKAGE.ordinal());
        String definitionKey = busBpmnInfo.getProcessDefinitionKey();
        BigDecimal discountPrice = ActivitiUtils.getDiscountAmount(processService.getBpmnModel(definitionKey,busBpmnInfo.getVersion()), definitionKey);
        return discountPrice;
    }

    /**
     * 查询最新的BpmnInfo对象
     * @param type
     * @return
     */
    @Override
    public BusBpmnInfo selectLastestInfoByType(int type) {

        return busBpmnInfoMapper.selectLastestBpmnInfoByType(type);
    }
}
